package com.mf.morefun.ui.userinfo;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.mf.morefun.R;
import com.mf.morefun.base.BaseBusActivity;
import com.mf.morefun.bean.MessageBean;
import com.mf.morefun.constant.Configuration;
import com.mf.morefun.requestbean.RequestMessageBean;
import com.mf.morefun.ui.dohomework.HomeworkListActivity;
import com.mf.morefun.widget.NetErrorPop;
import com.mf.morefun.widget.RoundProcessDialog;
import com.zhcx.zhcxlibrary.utils.StringUtils;
import com.zhcx.zhcxlibrary.utils.ToastUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FirstPasswordActivity extends BaseBusActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.tv_next)
    TextView tvNext;
    private RoundProcessDialog dialog;

    @Override
    protected int getNaviteColor() {
        return 0;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_firstpassword;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        dialog = new RoundProcessDialog(FirstPasswordActivity.this, "正在发送...");
    }

    @Override
    protected void initData() {

    }

    //获取短信验证吗
    private void initGetMessageHttp(String user) {
        if (null != dialog) {
            dialog.show();
        }
        RequestParams params = new RequestParams(Configuration.HOST + Configuration.GETMESSAGECODE);
        RequestMessageBean bean = new RequestMessageBean();
        bean.setMobile(user);
        params.setBodyContent(JSON.toJSONString(bean));
        params.setCharset("UTF-8");
        params.addHeader("Content-Type", "application/json");
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (null != dialog) {
                    dialog.cancel();
                }
                if (!StringUtils.isEmpty(result)) {
                    MessageBean messageBean = JSON.parseObject(result, MessageBean.class);
                    if (messageBean.getErrorCode() == 0) {
                        Intent mIntent = new Intent(FirstPasswordActivity.this, SecondPasswordActivity.class);
                        mIntent.putExtra("phone", user);
                        mIntent.putExtra("Verification", messageBean.getResult().getVarifyCode());
                        startActivity(mIntent);
                    } else {
                        ToastUtils.show(getApplicationContext(), messageBean.getMessage(), 100);
                    }
                } else {
                    ToastUtils.show(getApplicationContext(), "短信发送失败", 100);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                if (null != dialog) {
                    dialog.cancel();
                }
                mHandler.sendEmptyMessage(101);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 101:
                    NetErrorPop errorPop = new NetErrorPop(FirstPasswordActivity.this);
                    errorPop.showPop("");
                    break;
            }
        }
    };

    @OnClick({R.id.iv_back, R.id.tv_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_next:
                if (StringUtils.isEmpty(editPhone.getText().toString())) {
                    ToastUtils.show(getApplicationContext(), "手机不能为空", 100);
                    return;
                }
                if (editPhone.getText().toString().length() != 11 || !editPhone.getText().toString().substring(0, 1).equals("1")) {
                    ToastUtils.show(getApplicationContext(), "请输入正确的手机号码", 100);
                    return;
                }
                initGetMessageHttp(editPhone.getText().toString());
                break;
        }
    }
}
